#include "enemy1_1x1.h"
sprite* createEnemy1_1x1() {
    Enemy1_1x1* pEnemy1 = new Enemy1_1x1;
    return (sprite*)pEnemy1;
}
// 构造函数
Enemy1_1x1::Enemy1_1x1() {
    this->width = GRID_WIDTH;
    this->height = GRID_HEIGHT;
    this->isCollisionable = true;
    this->spriteType = sprite_type_enemy1_1x1;
    this->shootnum = 0;//射击频率
    this->isShoot = false;
    // 生命值、状态等初始化保持不变
    this->life = 100;
    this->status = enemy1_normal0; // 状态0
    this->statusTimeInterval = 0; // 用于间隔计数
    // 分配原图形二级指针内存
    imgenemy1_1x1 = new IMAGE * [2];
    for (int i = 0; i < 2; i++) {
        imgenemy1_1x1[i] = new IMAGE;
        char strImgPath[100];
        sprintf_s(strImgPath, "img/enemy1/enemy1_1x1_%d.png", i);
        loadimage(this->imgenemy1_1x1[i], strImgPath);
    }
    // 分配掩码二级指针内存
    imgenemy1_1x1_mask = new IMAGE * [2];
    for (int i = 0; i < 2; i++) {
        imgenemy1_1x1_mask[i] = new IMAGE;
        char strImgPath[100];
        sprintf_s(strImgPath, "img/enemy1/imgenemy1_1x1_%d_mask.png", i);
        loadimage(this->imgenemy1_1x1_mask[i], strImgPath);
    }
}
// 析构函数
Enemy1_1x1::~Enemy1_1x1() {
    // 首先遍历并删除imgenemy1_1x1数组中的每个IMAGE对象，然后删除imgenemy1_1x1二级指针本身。
    // 对imgenemy1_1x1_mask数组及指针做同样的操作，释放其分配的内存。
    for (int i = 0; i < 2; i++) {
        // 释放原图形内存
        delete this->imgenemy1_1x1[i];
        // 释放掩码内存
        delete this->imgenemy1_1x1_mask[i];
    }
    // 释放原图形二级指针内存
    delete[] this->imgenemy1_1x1;
    // 释放掩码二级指针内存
    delete[] this->imgenemy1_1x1_mask;
    // 释放攻击数组中的bolt1对象
    for (bolt1* attack : vecBolt1) {
        delete attack;
    }
}
// 绘制函数
void Enemy1_1x1::draw() {
    // 根据当前状态选择图片绘制
    putTransparentImage(this->x, this->y, this->imgenemy1_1x1_mask[this->status], this->imgenemy1_1x1[this->status]);
    settextstyle(20, 0, "宋体");
    char buff[15];
    sprintf_s(buff, "Enemy:%d", this->life);
    outtextxy(this->x - 5, this->y - 20, buff);
}
// 更新函数
void Enemy1_1x1::update() {
    if (this->life > 0) {
        this->statusTimeInterval++;
        if (this->statusTimeInterval == 15) {
            if (this->status == enemy1_normal0) {
                this->status = enemy1_normal1;
            }
            else if (this->status == enemy1_normal1) {
                this->status = enemy1_normal0;
            }
            this->statusTimeInterval = 0;
        }
    }
    // 遍历子弹数组，调用每一个子弹的draw函数
    for (bolt1* pBolt1 : vecBolt1) {
        pBolt1->globalY = this->globalY + 24;
        pBolt1->y = this->y + 24;
        pBolt1->update();
        pBolt1->draw();
    }
}
// 获取坐标并检查是否攻击，同时销毁无效攻击
void Enemy1_1x1::getCoordinatesAndCheckAttack(sprite* pMario) {
    if (this->life > 0) {
        int distanceX = abs(pMario->globalX - this->globalX);
        int distanceY = abs(pMario->globalY - this->globalY);
        // 使用固定的检测范围，并且要求y坐标差值为<=GRID_WIDTH
        ++shootnum;
        if (distanceX <= DetectionRange && distanceY >= 0 && distanceY < 10) {
            this->isShoot = true; // 攻击标志
            if (shootnum > 6) {
                this->generateBolt1(pMario);//生成攻击
                shootnum = 0;
            }
        }
        else {
            this->isShoot = false;
        }
    }
    // 销毁无效攻击（超出屏幕范围为无效）
    for (int i = 0; i < vecBolt1.size(); ++i) {
        bolt1* pBolt1 = vecBolt1.at(i);
        // 判断是否超出屏幕范围
        if (pBolt1->globalX <= 0 || pBolt1->globalX >= VIEW_WIDTH) {
            delete pBolt1;
            this->vecBolt1.erase(vecBolt1.begin() + i);
        }
        else {
            POINT bolt1Point; // 用于存储子弹可能碰到角色的点的坐标
            RECT marioRect; // 定义一个RECT类型的变量，用于存储主角的躯干矩形范围
            marioRect.left = pMario->globalX + 20; // 矩形的左边界与主角的左侧位置重合
            marioRect.top = pMario->globalY + 8; // 矩形的上边界与主角的顶部位置重合
            marioRect.right = pMario->globalX + 39; // 矩形的右边位置与主角的右边位置重合
            marioRect.bottom = pMario->globalY + 56; // 矩形的下边位置与主角的下边位置重合

            // 检查子弹四个角是否在角色矩形内
            //左上角
            bolt1Point.x = pBolt1->globalX;
            bolt1Point.y = pBolt1->globalY;
            // PtInRect用于判断这个点bolt1Point是否在这个矩形marioRect内，如果在矩形内则返回true，反之
            if (PtInRect(&marioRect, bolt1Point)) {
                pMario->life -= 5;
                delete pBolt1;
                vecBolt1.erase(vecBolt1.begin() + i);
                continue;
            }
            // 右上角
            bolt1Point.x = pBolt1->globalX + 33;
            bolt1Point.y = pBolt1->globalY;
            if (PtInRect(&marioRect, bolt1Point)) {
                pMario->life -= 5;
                delete pBolt1;
                vecBolt1.erase(vecBolt1.begin() + i);
                continue;
            }
            // 左下角
            bolt1Point.x = pBolt1->globalX;
            bolt1Point.y = pBolt1->globalY + 24;
            if (PtInRect(&marioRect, bolt1Point)) {
                pMario->life -= 5;
                delete pBolt1;
                vecBolt1.erase(vecBolt1.begin() + i);
                continue;
            }
            // 右下角
            bolt1Point.x = pBolt1->globalX + 33;
            bolt1Point.y = pBolt1->globalY + 24;
            if (PtInRect(&marioRect, bolt1Point)) {
                pMario->life -= 5;
                delete pBolt1;
                vecBolt1.erase(vecBolt1.begin() + i);
                continue;
            }
        }
    }
    return;
}
// 攻击函数
void Enemy1_1x1::generateBolt1(sprite* pMario) {
    bolt1* pBolt1 = new bolt1;
    // 初始化子弹的坐标和速度
    if (pMario->globalX >= this->globalX + GRID_WIDTH) {
        pBolt1->vx = 10; // 子弹速度变为10
        pBolt1->x = this->x + 33;
        pBolt1->y = this->y + 24;
        // 子弹在整个场景的位置也需要更新
        pBolt1->globalX = this->globalX + 33;
        pBolt1->globalY = this->globalY + 24;
    }
    else if (pMario->globalX <= this->globalX) {
        pBolt1->vx = -10;//子弹速度变为-10
        pBolt1->x = this->x - 33;
        pBolt1->y = this->y + 24;
        // 子弹在整个场景的位置也需要更新
        pBolt1->globalX = this->globalX - 33;
        pBolt1->globalY = this->globalY + 24;

    }
    vecBolt1.push_back(pBolt1);
    return;
}